﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace User_subsystem
{
    public partial class FormAdminLookUser : Form
    {
        public FormAdminLookUser()
        {
            InitializeComponent();
        }
        //用户修改信息
        public static string name;
        public static string id;
        public static string address;
        public static string phone;
        public static string select_id;
        private void 关键字_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void LoadUserInfo()
        {
            dataGridViewUser.Rows.Clear();
            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            string sql = $"select  UserID,UserName,ContactNum,Address,Status" +
                $" from [UserInfor]";
            SqlDataReader reader = dao.read(sql);

            while (reader.Read())
            {
                dataGridViewUser.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(),
                    reader[4].ToString()
                    );
            }
            reader.Close();
            dao.DaoClose();
        }
        private void FormAdmin_LookUser_Load(object sender, EventArgs e)
        {
            //显示所有的用户信息  密码除外
            LoadUserInfo();   
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LoadUserInfo();
            MessageBox.Show("刷新成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void lblID_Click(object sender, EventArgs e)
        {

        }

        private void FormAdmin_LookUser_Click(object sender, EventArgs e)
        {

        }

        private void dataGridViewUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //如果点击的这一行本身就为空，或者填充的数据为空，把当前选中改成null
            if (dataGridViewUser.CurrentRow == null || dataGridViewUser.CurrentRow.Cells[0].Value == null)
            {
                lblID.Text = "NULL";
                lblID.Text = "NULL";
            }
            else //不为空，要把当前选中改成选中部分
            {
                select_id= dataGridViewUser.CurrentRow.Cells[0].Value.ToString();
                lblID.Text = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
                FormAdminLookUser.name = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
                FormAdminLookUser.address  = dataGridViewUser.CurrentRow.Cells[3].Value.ToString();
                FormAdminLookUser.phone  = dataGridViewUser.CurrentRow.Cells[2].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1.获取关键字   进行模糊查询  用户表中的字段
            string key = txtKey.Text.Trim();

            //2.清空网格数据，显示新的数据到网格控件当中
            dataGridViewUser.Rows.Clear();
            DaoAdmin dao = new DaoAdmin();
            dao.connect();

            string sql = $"select  UserID,UserName,ContactNum,Address,Status  from [UserInfor]" +  
                $"where [UserID] like '%{key}%' or UserName like '%{key}%' or [Address] like '%{key}%' "+
                $"or ContactNum like '%{key}%' or Status  like '%{key}%' ";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dataGridViewUser.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(),
                   reader[4].ToString()
                   );
            }

            //3.关闭读取器，数据库连接
            reader.Close();
            dao.DaoClose();


        }

        private void button7_Click(object sender, EventArgs e)
        {
            //获取到当前选中的用户编号 key
            if (lblID.Text == "NULL")
            {
                MessageBox.Show("未选中用户！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (DialogResult.Yes == MessageBox.Show("确认删除吗？", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                { 
                    if(select_id.Equals("1000"))
                    {
                        MessageBox.Show("该管理员不允许删除", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else 
                    { 
                        //删除对应数据
                        DaoAdmin dao=new DaoAdmin();
                        dao.connect();
                        string sql = $"delete from UserInfor where UserID='{select_id}'";
                        dao.command(sql);
                       
                        if(dao.Execute(sql) > 0)
                        {
                            MessageBox.Show("删除该用户成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            MessageBox.Show("删除失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }

            }
        }
    }
}
